(function() {

    var postParams = {};
    postParams.landlordId = getArgs().landlordId;

    //点击弹框的 确定 按钮，弹框消失
    $('.weui_btn_dialog.primary').on('click',function() {
        $(this).parents('.weui_dialog_alert').hide();
    });


    //page1 点击基本信息页面 下一步
    //var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    var myreg = /^1\d{10}$/;
    var idCardReg = /^\d{17}[\d|X|x]$/;
    $('.base-info .next-step').on('click',function() {
        console.log( typeof( $('.base-info input[name=idcard-num]').val().length ) );
        if( $('.base-info input[name=landlordName]').val() == '' ) {
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('姓名必填');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( $('.base-info input[name=idcard-num]').val() == '' ) {
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('身份证号必填');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( !idCardReg.test( $('.base-info input[name=idcard-num]').val() ) ) {
            $('.weui_dialog_bd.prompt_text').html('请输入正确身份证号');
            $('.weui_dialog_alert').show();
            return false;
        }
        if( $('.base-info input[name=back-name]').val() == '' ) {
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('收款银行必填');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( $('.base-info input[name=back-account]').val() == '' ) {
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('银行卡号必填');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( $('.base-info input[name=landlord-phone]').val() == '' ) {
            $(this).parents('.base-info').find('.weui_dialog_bd.prompt_text').html('电话号码必填');
            $(this).parents('.base-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( !myreg.test( $('.base-info input[name=landlord-phone]').val() ) ){
            $('.weui_dialog_bd.prompt_text').html('请输入正确手机号');
            $('.weui_dialog_alert').show();
            $('.base-info input[name=landlord-phone]').val('');
            return false;
        }
        //获取用户填写的信息
        postParams.landlordName = $('.base-info input[name=landlordName]').val();
        postParams.idCardNo = $('.base-info input[name=idcard-num]').val();
        postParams.bankName = $('.base-info input[name=back-name]').val();
        postParams.bankAccount = $('.base-info input[name=back-account]').val();
        postParams.phone = $('.base-info input[name=landlord-phone]').val();

        console.log( postParams );
         $(this).parents('.base-info').hide();
         $('.upload-idcard').show();
    });

    //page2
    var positiveIdCard ;
    $(".upload-idcard .id-card-01").uploadImage({
        url: global.uip.photoUrl,// globalUploadHost + uploadUrl,
        idCard: "8a28cd6e586797fc015867a6a2eb001d",//landlordUserInfo.id,
        preAddress:global.uip.preAddress,// imageAddress,
        defaultImage: "../../../public/img/shenzhengmian@2x.png",
        fileImage: positiveIdCard,
        /*是否启动自增自减*/
        autoAppend: false,
        /*如果启动了自增减，那么这个参数必须传*/
        autoMax: 3,
        onchange: function($el) {
            var list = $(".upload-idcard .id-card-01 .uploadFileAddress")[0], string;
            if (list) {
                string = list.value;
            }
            $(".upload-idcard input[name='landlord.positiveIdCard']").val(string);
        }
    });

    var backIdCard ;
    $(".upload-idcard .id-card-02").uploadImage({
        url: global.uip.photoUrl,//globalUploadHost + uploadUrl,
        idCard: "8a28cd6e586797fc015867a6a2eb001d",//landlordUserInfo.id,

        defaultImage: "../../../public/img/shenfenzheng@2x.png",
        preAddress: global.uip.preAddress,//imageAddress,
        fileImage: backIdCard,

        onchange: function($el) {
            var list = $(".upload-idcard .id-card-02 .uploadFileAddress")[0], string;
            if (list) {
                string = list.value;
            }
            $(".upload-idcard input[name='landlord.backIdCard']").val(string);
        }

    });

    var takeIdCard;
    var idcardPicIndex = 3;
    $(".upload-idcard .id-card-03").uploadImage({
        url: global.uip.photoUrl,//globalUploadHost + uploadUrl,
        idCard: "8a28cd6e586797fc015867a6a2eb001d",//landlordUserInfo.id,
        defaultImage: "../../../public/img/handleCard@2x.png",
        preAddress: global.uip.preAddress,//imageAddress,
        fileImage: takeIdCard,
        onchange: function($el) {
            var list = $(".upload-idcard .id-card-03 .uploadFileAddress")[0], string;
            if (list) {
                string = list.value;
                idcardAddPic();
            }
            $(".upload-idcard input[name='certificate_03']").val(string);

        }
    });

    function idcardAddPic() {
        idcardPicIndex++;
        var dom = '<div class="certificate id-card-0'+ idcardPicIndex +'" style="background: #fff;"></div><input type="hidden" name="certificate_0'+ idcardPicIndex +'">';
        $('.upload-idcard .main .mod-upload-pic .item').append( dom );

        $( ".upload-idcard .id-card-0"+idcardPicIndex ).uploadImage({
            url: global.uip.photoUrl,//globalUploadHost + uploadUrl,
            idCard: "8a28cd6e586797fc015867a6a2eb001d",//landlordUserInfo.id,
            defaultImage: "../../../public/img/handleCard@2x.png",
            preAddress: global.uip.preAddress,//imageAddress,
            fileImage: takeIdCard,
            onchange: function($el) {
                var list = $(".upload-idcard .id-card-0"+ idcardPicIndex +" .uploadFileAddress")[0], string;
                if (list) {
                    string = list.value;
                }
                $(".upload-idcard input[name='certificate_0"+ idcardPicIndex +"']").val(string);
                idcardAddPic();
            }
        });
    }

    //点击上传身份证页面 下一步
    $('.upload-idcard .next-step').on('click',function() {
        var landlordPositiveIdCard = $('.upload-idcard input[name="landlord.positiveIdCard"]').val();
        var landlordBackIdCard = $('.upload-idcard input[name="landlord.backIdCard"]').val();
        var landlordHouseCertificate = $('.upload-idcard input[name="certificate_03"]').val();
        var certificateNum = $('.certificate').length;
        console.log( certificateNum );

        var imgArr = [];
        for( var i=0; i<certificateNum-1; i++ ) {
            var index = i+3;
            var imgStr = global.uip.preAddress+$('input[name="certificate_0'+ index +'"]').val();
            imgArr.push( imgStr );
        }
        console.log( imgArr );


        if( !landlordPositiveIdCard ) {
            $(this).parents('.upload-idcard').find('.weui_dialog_bd.prompt_text').html('请上传身份证正面照');
            $(this).parents('.upload-idcard').find('.weui_dialog_alert').show();
            return false;
        }
        if( !landlordBackIdCard ) {
            $(this).parents('.upload-idcard').find('.weui_dialog_bd.prompt_text').html('请上传身份证反面照');
            $(this).parents('.upload-idcard').find('.weui_dialog_alert').show();
            return false;
        }
        if( !landlordHouseCertificate ) {
            $(this).parents('.upload-idcard').find('.weui_dialog_bd.prompt_text').html('请上传房产证复印件');
            $(this).parents('.upload-idcard').find('.weui_dialog_alert').show();
            return false;
        }

        postParams.idCard = global.uip.preAddress+landlordPositiveIdCard;
        postParams.idCardBack = global.uip.preAddress+landlordBackIdCard;
        postParams.house = imgArr;
        console.log( postParams );
        $('.landlord.pop').show();
    });

    var hasConsignor;
    $('.landlord.pop .popBottom .confirm').on('click',function() {
        hasConsignor = true;
        $('.upload-idcard').hide();
        $(this).parents('.landlord.pop').hide();
        $('.consignor-info').show();
        $('.member-info.parties-info').show();
    });

    $('.landlord.pop .popBottom .cancel').on('click',function() {
        hasConsignor = false;
        $(this).parents('.upload-idcard').hide();
        $(this).parents('.landlord.pop').hide();
        $('.all-info').show();
        renderAllInfo();
        $('.member-info.parties-info').hide();
    });

    //page3
    var positiveIdCard ;
    $(".consignor-info .id-card-01").uploadImage({
        url: global.uip.photoUrl,// globalUploadHost + uploadUrl,
        idCard: "8a28cd6e586797fc015867a6a2eb001d",//landlordUserInfo.id,
        preAddress:global.uip.preAddress,// imageAddress,
        defaultImage: "../../../public/img/shenzhengmian@2x.png",
        fileImage: positiveIdCard,
        /*是否启动自增自减*/
        autoAppend: false,
        /*如果启动了自增减，那么这个参数必须传*/
        autoMax: 3,
        onchange: function($el) {
            var list = $(".consignor-info .id-card-01 .uploadFileAddress")[0], string;
            if (list) {
                string = list.value;
            }
            $(".consignor-info input[name='consignor.positiveIdCard']").val(string);
        }
    });

    var backIdCard ;
    $(".consignor-info .id-card-02").uploadImage({
        url: global.uip.photoUrl,//globalUploadHost + uploadUrl,
        idCard: "8a28cd6e586797fc015867a6a2eb001d",//landlordUserInfo.id,

        defaultImage: "../../../public/img/shenfenzheng@2x.png",
        preAddress: global.uip.preAddress,//imageAddress,
        fileImage: backIdCard,
        onchange: function($el) {
            var list = $(".consignor-info .id-card-02 .uploadFileAddress")[0], string;
            if (list) {
                string = list.value;
            }
            $(".consignor-info input[name='consignor.backIdCard']").val(string);
        }
    });

    var takeIdCard;
    var picIndexConsignor = 3;
    $(".consignor-info .id-card-03").uploadImage({
        url: global.uip.photoUrl,//globalUploadHost + uploadUrl,
        idCard: "8a28cd6e586797fc015867a6a2eb001d",//landlordUserInfo.id,
        defaultImage: "../../../public/img/handleCard@2x.png",
        preAddress: global.uip.preAddress,//imageAddress,
        fileImage: takeIdCard,
        onchange: function($el) {
            var list = $(".consignor-info .id-card-03 .uploadFileAddress")[0], string;
            if (list) {
                string = list.value;
            }
            $(".consignor-info input[name='consignor_03']").val(string);
            consignorAddPic();
        }
    });

    function consignorAddPic() {
        picIndexConsignor++;
        var dom = '<div class="consignor id-card-0'+ picIndexConsignor +'" style="background: #fff;"></div><input type="hidden" name="consignor_0'+ picIndexConsignor +'">';
        $('.consignor-info .main .mod-upload-pic .item').append( dom );
        $( ".consignor-info .id-card-0"+picIndexConsignor ).uploadImage({
            url: global.uip.photoUrl,//globalUploadHost + uploadUrl,
            idCard: "8a28cd6e586797fc015867a6a2eb001d",//landlordUserInfo.id,
            defaultImage: "../../../public/img/handleCard@2x.png",
            preAddress: global.uip.preAddress,//imageAddress,
            fileImage: takeIdCard,
            onchange: function($el) {
                var list = $(".consignor-info .id-card-0"+ picIndexConsignor +" .uploadFileAddress")[0], string;
                if (list) {
                    string = list.value;
                }
                $(".consignor-info input[name='consignor_0"+ picIndexConsignor +"']").val(string);
                consignorAddPic();
            }
        });
    }

    //点击委托人信息页面 下一步
    $('.consignor-info .next-step').on('click',function() {
        //

        var consignorMobile = $('.consignor-info input[name=consignorMobile]').val();
        var consignorPositiveIdCard = $('.consignor-info input[name="consignor.positiveIdCard"]').val();
        var consignorBackIdCard = $('.consignor-info input[name="consignor.backIdCard"]').val();
        var consignorHouseCertificate = $('.consignor-info input[name="consignor_03"]').val();

        var consignorNum = $('.consignor').length;
        console.log( consignorNum );

        var imgArr = [];
        for( var i=0; i<consignorNum-1; i++ ) {
            var index = i+3;
            var imgStr = global.uip.preAddress+$('input[name="consignor_0'+ index +'"]').val();
            imgArr.push( imgStr );
        }
        //var houseImgs = imgArr.join(',');
        var myreg = /^1\d{10}$/;
        console.log( imgArr );

        if( !consignorMobile ) {
            $(this).parents('.consignor-info').find('.weui_dialog_bd.prompt_text').html('请上传手机号');
            $(this).parents('.consignor-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( !myreg.test(consignorMobile) ) {
            $(this).parents('.consignor-info').find('.weui_dialog_bd.prompt_text').html('请填写正确的手机号');
            $(this).parents('.consignor-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( !consignorPositiveIdCard ) {
            $(this).parents('.consignor-info').find('.weui_dialog_bd.prompt_text').html('请上传身份证正面照');
            $(this).parents('.consignor-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( !consignorBackIdCard ) {
            $(this).parents('.consignor-info').find('.weui_dialog_bd.prompt_text').html('请上传身份证反面照');
            $(this).parents('.consignor-info').find('.weui_dialog_alert').show();
            return false;
        }
        if( !consignorHouseCertificate ) {
            $(this).parents('.consignor-info').find('.weui_dialog_bd.prompt_text').html('请上传授权书');
            $(this).parents('.consignor-info').find('.weui_dialog_alert').show();
            return false;
        }

        postParams.clientPhone = consignorMobile;
        postParams.clientCard = global.uip.preAddress + consignorPositiveIdCard;
        postParams.clientCardBack = global.uip.preAddress + consignorBackIdCard;
        postParams.entrust = imgArr;

        console.log( postParams );

         $(this).parents('.consignor-info').hide();
         $('.all-info').show();

        renderAllInfo();
    });

    function renderAllInfo() {
        // baseInfo
        $('.all-info span[name="landlord-name"]').html( $('.base-info input[name=landlordName]').val() );
        $('.all-info span[name="landlord-idcard"]').html( $('.base-info input[name=idcard-num]').val() );
        $('.all-info span[name="landlord-backName"]').html( $('.base-info input[name=back-name]').val() );
        $('.all-info span[name="landlord-backAccount"]').html( $('.base-info input[name=back-account]').val() );
        $('.all-info span[name="landlord-phoneNum"]').html( $('.base-info input[name=landlord-phone]').val() );
        // upload-idcard
        $('.all-info img[name="positive-idcard"]').attr( 'src',global.uip.preAddress+$(".upload-idcard input[name='landlord.positiveIdCard']").val() );
        $('.all-info img[name="back-idcard"]').attr( 'src',global.uip.preAddress+$(".upload-idcard input[name='landlord.backIdCard']").val() );

        var certificateNum = $('.certificate').length;
        var dom = '';
        var index=0;
        for( var i=0; i<certificateNum-1; i++ ) {
            index = i+3;
            dom += '<li>'
                     +   '<img src="'+global.uip.preAddress+$('input[name="certificate_0'+ index +'"]').val()+'"/>'
                     +   '<span class="hidden"><img src="'+global.uip.preAddress+$('input[name="certificate_0'+ index +'"]').val()+'"/>'
                     +   '</span>'
                     + '</li>';
        }
        $('.all-info .kePublic.landlord-wrapper .msKeimgBox ul').append(dom);

        //委托人信息
        $('.all-info span[name="consignorPhone"]').html( $('.consignor-info input[name=consignorMobile]').val() );
        $('.all-info img[name="consignor-positive-card"]').attr( 'src',global.uip.preAddress+$(".consignor-info input[name='consignor.positiveIdCard']").val() );
        $('.all-info img[name="consignor-back-card"]').attr( 'src',global.uip.preAddress+$(".consignor-info input[name='consignor.backIdCard']").val() );
        var consignorNum = $('.consignor').length;
        console.log( consignorNum );
        var index2 = 0;
        var dom2 = '';
        for( var j=0; j<consignorNum-1; j++ ) {
            index2 = j+3;
            dom2 += '<li>'
                     +   '<img src="'+global.uip.preAddress+$('input[name="consignor_0'+ index2 +'"]').val() +'"/>'
                     +   '<span class="hidden"><img src="'+global.uip.preAddress+$('input[name="consignor_0'+ index2 +'"]').val()+'"/>'
                     +   '</span>'
                     +'</li>';
        }
        $('.all-info .kePublic.consignor-wrapper .msKeimgBox3 ul').append(dom2);
    }

    //page4 点击汇总展示页面 下一步
    $('.all-info .next-step').on('click',function() {
        var flag = $('#aleady-read').is(':checked');
        console.log( flag );
        if( !flag == 'false' ) {
            $(this).parents('.consignor-info').find('.weui_dialog_bd.prompt_text').html('请同意房租月付条款');
            $(this).parents('.consignor-info').find('.weui_dialog_alert').show();
            return false;
        }
        postContract(postParams);
    });

    //ajax
    function postContract(param) {
        var methodId = "4005";
        var info = postdata(methodId,param);
        $.ajax({
                url: global.uip.url,
                type: "post",
                data: info,
                success: function (jsonData) {
                    if (jsonData.code == "0") {
                        window.location.href = 'checking.html';
                    } else {
                        console.log( "4005",jsonData )
                        $('.all-info .weui_dialog_bd.prompt_text').html( jsonData.res.msg );
                        $('.all-info .weui_dialog_alert').show();
                    }
                },
                error: function () {
                    alert("接口错误");
                }
            });
    }

    //historyBack
    $('.upload-idcard.ghy').on('click','.back-button',function(){
        $(this).parents('.upload-idcard.ghy').hide();
        $('.base-info').show();
    });
    $('.consignor-info').on('click','.back-button',function(){
        $(this).parents('.consignor-info').hide();
        $('.upload-idcard.ghy').show();
    });
    $('.all-info').on('click','.back-button',function(){
        if( hasConsignor ) {
            $(this).parents('.all-info').hide();
            $('.consignor-info').show();
        } else {
           $(this).parents('.all-info').hide();
           $('.upload-idcard.ghy').show();
        }
    });

    //点击查询授权书
    // $('.check-cluase').on('click', function () {
    //     location.href = "contractDetail.html";
    // });

})();
